package com.billy.ohos.loadingstatusview.wrapability.adapter;

import com.billy.ohos.loading.Gloading;
import com.billy.ohos.loadingstatusview.util.Constants;
import com.billy.ohos.loadingstatusview.wrapability.adapter.view.GlobalLoadingStatusView;
import ohos.agp.components.Component;

/**
 * demo to show how to create a {@link Gloading.Adapter}
 *
 * @author billy.qi
 * @see GlobalLoadingStatusView
 * @since 19/3/18 18:37
 */
public class GlobalAdapter implements Gloading.Adapter {

    @Override
    public Component getComponent(Gloading.Holder holder, Component convertComponent, int status) {
        GlobalLoadingStatusView loadingStatusView = null;
        // reuse the old view, if possible
        if (convertComponent != null && convertComponent instanceof GlobalLoadingStatusView) {
            loadingStatusView = (GlobalLoadingStatusView) convertComponent;
        }
        if (loadingStatusView == null) {
            loadingStatusView = new GlobalLoadingStatusView(holder.getContext(), holder.getRetryTask());
        }
        loadingStatusView.setStatus(status);
        // show or not show msg view
        Object data = holder.getData();
        boolean hideMsgView = Constants.HIDE_LOADING_STATUS_MSG.equals(data);
        loadingStatusView.setMsgViewVisibility(!hideMsgView);
        return loadingStatusView;
    }
}
